package com.morehair.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.wx.WxFootprintBo;
import com.morehair.bean.pojo.Footprint;
import com.morehair.bean.pojo.FootprintExample;
import com.morehair.bean.pojo.Goods;
import com.morehair.bean.vo.wx.WxFootprintVo;
import com.morehair.mapper.FootprintMapper;
import com.morehair.mapper.GoodsMapper;
import com.morehair.service.wx.WxFootprintService;
import com.morehair.service.wx.WxFootprintService;
import com.morehair.utils.GetNowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WxFootprintServiceImp implements WxFootprintService {

    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxFootprintVo getFootprint(WxFootprintBo footprintBo) {

        PageHelper.startPage(footprintBo.getPage(), footprintBo.getSize());

        Integer userId = GetNowUser.getUser().getId();

        WxFootprintVo footprintVo = new WxFootprintVo();
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        ArrayList<WxFootprintVo.FootprintListBean> footprintListBeans = new ArrayList<>();

        for (Footprint footprint : footprints) {

            WxFootprintVo.FootprintListBean footprintListBean = new WxFootprintVo.FootprintListBean();
            footprintListBean.setAddTime(footprint.getAddTime());
            footprintListBean.setId(footprint.getId());
            Integer goodsId = footprint.getGoodsId();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            footprintListBean.setGoodsId(goodsId);
            footprintListBean.setBrief(goods.getBrief());
            footprintListBean.setName(goods.getName());
            footprintListBean.setPicUrl(goods.getPicUrl());
            footprintListBean.setRetailPrice(goods.getRetailPrice());
            footprintListBeans.add(footprintListBean);

        }
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        int size = footprintPageInfo.getSize();
        footprintVo.setTotalPages(size);
        footprintVo.setFootprintList(footprintListBeans);
        return footprintVo;
    }

    @Override
    public void deleteFootprint(Map map) {
        Integer id = (Integer) map.get("id");
        footprintMapper.fakeDeleteById(id, new Date());
    }
}
